package com.suncaper.tddmall.controller;

import com.suncaper.tddmall.common.model.Category;

import com.suncaper.tddmall.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/index")
public class SearchController {

    @Autowired
    SearchService searchService;
    @GetMapping("/search")
    public String searchByKeywords(@RequestParam("keywords") String keywords,@RequestParam("maxPrice") Integer maxPrice,@RequestParam("minPrice") Integer minPrice,Model model) {
        if (!keywords.isEmpty()){
            List<Category> categoryList = searchService.searchByKeywords(keywords);
            System.out.println(categoryList.size());
            model.addAttribute("categoryList",categoryList);

            return "../static/page/shop";
        }
        if (maxPrice!=null&&minPrice!=null){
            List<Map> listMap = searchService.searchByPrice(maxPrice,minPrice);
            model.addAttribute("goodsList",listMap);
            return "../static/page/shop";
        }
        List<Category> categoryList = searchService.searchByKeywords(keywords);
        System.out.println(categoryList.size());
        model.addAttribute("categoryList",categoryList);

        for (Category category:
                categoryList) {
            System.out.println(category.toString());
        }

        return "../static/page/shop";
    }

//    @Autowired
//    private SearchService searchService;
//    @Autowired
//    private PageService pageService;
//    @GetMapping("/search")
//    public String SearchByKeywords(Model model,@RequestParam("keywords") String keywords){
//        System.out.println(keywords);
//        List<Category> searchList = searchService.searchByKeywords(keywords);
//        Integer count = pageService.countSearchByKeywords(keywords);
//        //把查询到的数量封装到 ShopPages中
//        ShopPages shopPages = new ShopPages();
//        shopPages.setPageSize(count);
//
//        System.out.println(count);
//
//        model.addAttribute("searchList",searchList);
//        model.addAttribute("pages",shopPages);
//        return "../static/page/shop";
//    }
//
//    @GetMapping("/shop")
//    public String ShopPage(Model model,@RequestParam("msg") String keywords,@RequestParam("totalPage") String totalPage){
//        ShopPages shopPages = new ShopPages();
//        Integer count = pageService.countSearchByKeywords(keywords);
//        int pagenum = Integer.parseInt(totalPage);
//        shopPages.setTotalPage(pagenum);
//        shopPages.setPageSize(count);
//        List<Category> pagelist = pageService.searchByPageLimit(keywords,shopPages.getTotalPage());
//        model.addAttribute("keywords",keywords);
//        model.addAttribute("pagenum",pagenum);
//        //测试
//        Category category = new Category();
//        category = pagelist.get(1);
//        System.out.println(pagelist.size());
//        System.out.println(category.toString());
//
//        return "../static/page/shop";
//    }
    @RequestMapping("/demo")
    public String demo(){

        return "../static/page/order_detil";
    }
}
